/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.pagetransformer;

import com.to.aboomy.banner.BannerViewPager;
import ohos.agp.components.Component;

/**
 * RotateUpTransformer
 *
 * @since 2021-08-06
 */
public class RotateUpTransformer implements BannerViewPager.PageTransformer {
    private static final float ROT_MOD = -15.0F;
    private static final float NUM_F_0_5 = 0.5f;

    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {
        float width = (float) page.getWidth();
        float rotation = 0F;
        switch (pageType) {
            case PREVIOUS:
                if (offsetPixels < 0) {
                    float previousFloat = 1 - offset;
                    rotation = ROT_MOD * previousFloat;
                    page.setPivotX(width * NUM_F_0_5);
                    page.setPivotY(0.0F);
                    page.setTranslationX(0.0F);

                    if (offsetPixels < 0) {
                        page.setRotation(-rotation);
                    }
                }
                break;
            case CURRENT:
                rotation = ROT_MOD * offset;
                page.setPivotX(width * NUM_F_0_5);
                page.setPivotY(0.0F);
                page.setTranslationX(0.0F);
                if (offsetPixels >= 0) {
                    page.setRotation(-rotation);
                } else {
                    page.setRotation(rotation);
                }
                break;

            case NEXT:
                float nextFloat = 1F - offset;
                rotation = ROT_MOD * nextFloat;
                page.setPivotX(width * NUM_F_0_5);
                page.setPivotY(0.0F);
                page.setTranslationX(0.0F);

                if (offsetPixels >= 0) {
                    page.setRotation(rotation);
                }

                break;

            case OTHER:
                page.setPivotX(1F);
                page.setPivotY(1F);
                page.setRotation(0F);
                break;
            default:
                break;
        }
    }
}
